/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.web.friends;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.server.common.config.Global;
import com.server.common.persistence.Page;
import com.server.common.web.BaseController;
import com.server.common.utils.StringUtils;
import com.server.modules.sys.entity.friends.Friends;
import com.server.modules.sys.service.friends.FriendsService;

/**
 * 好友管理Controller
 * @author 王磊
 * @version 2016-09-02
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/friends/friends")
public class FriendsController extends BaseController {

	@Autowired
	private FriendsService friendsService;
	
	@ModelAttribute
	public Friends get(@RequestParam(required=false) String id) {
		Friends entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = friendsService.get(id);
		}
		if (entity == null){
			entity = new Friends();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:friends:friends:view")
	@RequestMapping(value = {"list", ""})
	public String list(Friends friends, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Friends> page = friendsService.findPage(new Page<Friends>(request, response), friends); 
		model.addAttribute("page", page);
		model.addAttribute("friends", friends);
		return "modules/sys/friends/friendsList";
	}
	@RequiresPermissions("sys:friends:friends:view")
	@RequestMapping(value = "form")
	public String form(Friends friends, Model model) {
		model.addAttribute("friends", friends);
		return "modules/sys/friends/friendsForm";
	}

	@RequiresPermissions("sys:friends:friends:edit")
	@RequestMapping(value = "save")
	public String save(Friends friends, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, friends)){
			return form(friends, model);
		}
		friendsService.save(friends);
		addMessage(redirectAttributes, "保存好友成功");
		return "redirect:"+Global.getAdminPath()+"/sys/friends/friends/?repage";
	}
	
	@RequiresPermissions("sys:friends:friends:edit")
	@RequestMapping(value = "delete")
	public String delete(Friends friends, RedirectAttributes redirectAttributes) {
		friendsService.delete(friends);
		addMessage(redirectAttributes, "删除好友成功");
		return "redirect:"+Global.getAdminPath()+"/sys/friends/friends/?repage";
	}

}